package com.ruoyi.web.controller.task;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * redis定期心跳(避免redis远程主机强迫关闭了一个现有连接)
 */
@Component("RedisHeartbeat")
public class RedisHeartbeatTask {

    private final RedisTemplate<String, String> redisTemplate;

    public RedisHeartbeatTask(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void sendHeartbeat() {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        opsForValue.set("heartbeat", "alive", 10L, TimeUnit.SECONDS);       //10秒过期
    }
}
